package com.bulemobi.activity.ac_guide;
import java.util.ArrayList;
import java.util.List;
import com.bulemobi.adapter.ViewPagerAdapter;
import com.bulemobi.view.BaseActivity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.jsyh.hcb.R;

/***
 * 
 * @author zengys
 * @date 2014-7-9 <br>
 *       引导界面
 * 
 */
public class GuideActivity extends BaseActivity implements
		View.OnClickListener, OnPageChangeListener {
	private ViewPager mViewPager;
	private ImageView[] mImageViews;
	private ViewPagerAdapter mPagerAdapter;
	private List<View> mListViews;
	private int mViewCount;
	private int mCurSel;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		 SharedPreferences sh = getSharedPreferences("guide",0);
          if(sh.getString("guides","1").endsWith("200")){
        	  openActivity(WelcomePageActivity.class);
        	  finish();
          }
		initViewPage();
		init();
	}
	
	 

	private void initViewPage() {
		mListViews = new ArrayList<View>();
		LayoutInflater mLayoutInflater = getLayoutInflater();

		mViewCount = 3;
		for (int i = 0; i < mViewCount; i++) {
			View view = mLayoutInflater.inflate(R.layout.pager_guide, null);
			view.setBackgroundColor((int) (Math.random() * Color.WHITE));
			mListViews.add(view);

			if (i == 0) {
				view.findViewById(R.id.btn_start).setVisibility(View.GONE);
				((ImageView) (view.findViewById(R.id.iv_guide)))
						.setImageResource(R.drawable.tt1);
			} else if (i == 1) {
				view.findViewById(R.id.btn_start).setVisibility(View.GONE);
				((ImageView) (view.findViewById(R.id.iv_guide)))
						.setImageResource(R.drawable.tt2);
			} else {
				view.findViewById(R.id.btn_start).setOnClickListener(this);
				((ImageView) (view.findViewById(R.id.iv_guide)))
						.setImageResource(R.drawable.tt3);
			}
		}
		mViewPager=(ViewPager) findViewById(R.id.viewpager);
		mPagerAdapter = new ViewPagerAdapter(mListViews);
		mViewPager.setAdapter(mPagerAdapter);
		mViewPager.setOnPageChangeListener(this);
	}

	private void init() {
		LinearLayout linearLayout = (LinearLayout) findViewById(R.id.llayout);

		mImageViews = new ImageView[mViewCount];

		for (int i = 0; i < mViewCount; i++) {
			mImageViews[i] = (ImageView) linearLayout.getChildAt(i);
			mImageViews[i].setEnabled(true);
			mImageViews[i].setOnClickListener(this);
			mImageViews[i].setTag(i);
		}

		mCurSel = 0;
		mImageViews[mCurSel].setEnabled(false);

	}

	private void setCurView(int pos) {
		if (pos < 0 || pos >= mViewCount) {
			return;
		}

		mViewPager.setCurrentItem(pos);
	}

	private void setCurPoint(int index) {
		if (index < 0 || index > mViewCount - 1 || mCurSel == index) {
			return;
		}

		mImageViews[mCurSel].setEnabled(true);
		mImageViews[index].setEnabled(false);

		mCurSel = index;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.btn_start) {
			SharedPreferences sh = getSharedPreferences("guide",MODE_PRIVATE);
			Editor editor = sh.edit();
			editor.putString("guides","200");
			editor.commit();
			//跳转到首页
			openActivity(WelcomePageActivity.class);
			GuideActivity.this.finish();
			return;
		}
		int pos = (Integer) v.getTag();
		setCurView(pos);
		setCurPoint(pos);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		setCurPoint(arg0);
	}

}
